<?php


function wpspo_load_color_picker_script() {
    wp_enqueue_script('farbtastic');
}
add_action('admin_print_scripts-widgets.php', 'wpspo_load_color_picker_script');

function wpspo_load_color_picker_style() {
    wp_enqueue_style('farbtastic');	
}
add_action('admin_print_styles-widgets.php', 'wpspo_load_color_picker_style');
    
/*
 * WIDGET JAUGE PROJETS
 */
class wpspo_widget_jauge extends WP_Widget {

    /**
     * Register widget with WordPress.
     */
    public function __construct() {
        parent::__construct(
            'wpspo_jauge_projet', // Base ID
            __('Progress Bar - SponsorPress ', 'sponsorpress'), // Name
            array( 'description' => __( 'Displays a progress bar for the current project', 'sponsorpress' ), ) // Args
        );
    }

    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance ) {

        global $post;
        global $current_user;
        extract( $args );
        $title = apply_filters( 'widget_title', $instance['title'] );
        $texte = apply_filters( 'widget_texte', $instance['texte'] );
        $color = apply_filters( 'widget_color', $instance['color'] );
        $type = apply_filters( 'widget_type', $instance['type'] );
        $display = apply_filters( 'widget_display', $instance['display'] );
        $choose = apply_filters( 'widget_display', $instance['choose'] );
        $printJauge = '';

        $afficheJauge = 0;
        
        if(is_singular('projet') && $choose==1) { $afficheJauge = 1; }
        if(is_singular('defi') && $choose==2) { $afficheJauge = 1; }
        
        if($post->ID && $afficheJauge == 1) {

            //$valueObjectif = get_post_meta($post->ID, 'wpspo_boxprojets', true); // Récupère l'objectif cible
            
            //if($valueObjectif>0) {
                //$totalPromesse = WPSPO_Promesse::get_sum($post->ID, 1);  // Récupère les promesses FIXES

                //$totalPromesse = 500;
                /*$ratio = round(( ($totalPromesse*100)/$valueObjectif ));
                $ratio2 = (95-$ratio);
                if($ratio2<(-15)) { $ratio2 = -15; }*/
              
                echo $before_widget;
                if ( ! empty( $title ) )
                    echo $before_title . $title . $after_title;
                //echo __( 'Hello, World!', 'sponsorpress' );
                echo do_shortcode('[wpspo-jauge id="'.$post->ID.'" choose="'.$choose.'" type="'.$type.'" text="'.$texte.'" display="'.$display.'" color="'.$color.'"]');
                if(get_the_author_id()==$current_user->ID && $post->post_status != 'archive') {
                    echo '<div style="text-align:center;margin-top:15px;"><form id="closedefi" name="closedefi" method="post" action="">';
                    wp_nonce_field('go-closedefi', 'security-closedefi');
                    echo '<input type="hidden" name="iddefi" value="'.$thePostID.'" />
                            <input type="submit" name="submit-closedefi"  style="display: inline-block;margin-top:10px;padding: .37em 1.5em; color: white;text-align:center;border-radius: 5px;text-shadow: none;background-color:#E15821;background: -webkit-linear-gradient(top,#E15821 0,#E15821 100%);text-decoration:none;border: 1px solid #E15821;" onClick="if(!confirm(\''.__('Close this challenge?', 'sponsorpress').'\')) return false;" value="'.__('Close this challenge?', 'sponsorpress').'"/>
                        </form></div>';
                }
                echo $after_widget;
            //}
        }
    }

    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update( $new_instance, $old_instance ) {

        $instance = array();
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['texte'] = strip_tags( $new_instance['texte'] );
        $instance['color'] = strip_tags( $new_instance['color'] );
        $instance['type'] = strip_tags( $new_instance['type'] );
        $instance['display'] = strip_tags( $new_instance['display'] );
        $instance['choose'] = strip_tags( $new_instance['choose'] );

        return $instance;
    }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form( $instance ) {

        if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'Progression', 'sponsorpress' ); }
        if ( isset( $instance[ 'texte' ] ) ) { $texte = $instance[ 'texte' ]; } else { $texte = __( 'Goal:', 'sponsorpress' ).' %OBJ% %SA%'.__('Amount collected : %COL%', 'sponsorpress' ); }
        if ( isset( $instance[ 'color' ] ) ) { $color = $instance[ 'color' ]; } else { $color = '#cc0000'; }
        if ( isset( $instance[ 'type' ] ) ) { $type = $instance[ 'type' ]; } else { $type = 2; }
        if ( isset( $instance[ 'display' ] ) ) { $display = $instance[ 'display' ]; } else { $display = 1; }
        if ( isset( $instance[ 'choose' ] ) ) { $choose = $instance[ 'choose' ]; } else { $choose = 1; }

        ?>
        <script type="text/javascript">

            //<![CDATA[
                jQuery(document).ready(function()
                {
                    // colorpicker field
                    jQuery('.color-picker-jauge').each(function(){
                        var $this = jQuery(this),
                            id = $this.attr('rel');

                        $this.farbtastic('#' + id);
                    });
                });
            //]]>   
          </script>
        <p>
            <label for="<?php echo $this->get_field_id( 'choose' ); ?>"><strong><?php _e( 'For:', 'sponsorpress' ); ?></strong></label><br />
            <input class="widefat" id="<?php echo $this->get_field_id( 'choose' ); ?>" name="<?php echo $this->get_field_name( 'choose' ); ?>" type="radio" value="1" <?php if($choose==1) { echo 'checked'; } ?>/><?php _e( 'Projects', 'sponsorpress' ); ?>
            <input class="widefat" id="<?php echo $this->get_field_id( 'choose' ); ?>" name="<?php echo $this->get_field_name( 'choose' ); ?>" type="radio" value="2" <?php if($choose==2) { echo 'checked'; } ?> /><?php _e( 'Challenges', 'sponsorpress' ); ?>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>"><strong><?php _e( 'Title:', 'sponsorpress' ); ?></strong></label> 
            <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'texte' ); ?>"><strong><?php _e( 'Text:', 'sponsorpress' ); ?></strong></label><br />
            <small><?php _e('Insert %COL% to display the total collected, %OBJ% to display the project objective and %SA% for newline.', 'sponsorpress'); ?></small>
        </p>
        <p>
            <textarea class="widefat" id="<?php echo $this->get_field_id( 'texte' ); ?>" name="<?php echo $this->get_field_name( 'texte' ); ?>"><?php echo esc_attr( $texte ); ?></textarea>
        </p>
        <p>
            <input type="checkbox" id="<?php echo $this->get_field_id( 'display' ); ?>" name="<?php echo $this->get_field_name( 'display' ); ?>" value="1" <?php if(esc_attr( $display )==1) { echo "checked"; } ?> /> <?php _e('Display jauge?', 'sponsorpress' ); ?></p>
        <p>
            <strong>Type :</strong> <br />
            <input type="radio" id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="1" <?php if(esc_attr( $type )==1) { echo "checked"; } ?> /> <?php _e('Horizontal progress', 'sponsorpress' ); ?><br />
            <input type="radio" id="<?php echo $this->get_field_id( 'type' ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>" value="2" <?php if(esc_attr( $type )==2) { echo "checked"; } ?> /> <?php _e('Circle progress', 'sponsorpress' ); ?><br />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'color' ); ?>"><strong><?php _e( 'Color:' , 'sponsorpress'); ?>(<?php echo esc_attr( $color ); ?>)</strong></label> 
            <input class="widefat" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" />
            <div class="color-picker-jauge" rel="<?php echo $this->get_field_id('color'); ?>"></div>

        </p>

        <?php 
    }

} // class Foo_Widget
//
// register Foo_Widget widget
add_action( 'widgets_init', create_function( '', 'register_widget( "wpspo_widget_jauge" );' ) );